
package com.kingdee.eas.custom.aoyuan.aytime.bizbill.web;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.waf.ctx.WafContext;
import com.kingdee.bos.waf.resource.Resources;
import com.kingdee.bos.waf.winlet.edit.BillEntryBean;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.HolidayTypeCollection;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.HolidayTypeFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.HolidayTypeInfo;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.HolidayBillEntryInfo;

@ManagedBean(name = "custom.aoyuan.aytime.bizbill.HolidayBillEntryBean", scope = ManagedBeanScope.SESSION)
public class HolidayBillEntryBean extends AbstractHolidayBillEntryBean {
	
	private List holidayTypeList;
	
    public void roadListModel()
    {
        try
        {
        	LongHolidayEdituiBean longHolidayEdituiBean = (LongHolidayEdituiBean) com.kingdee.bos.waf.util.BeanUtil
			.getBean("custom.aoyuan.aytime.bizbill.LongHolidayEdituiBean");	
        	Date startDate=(Date)longHolidayEdituiBean.txtStartdate.getValue();
        	Date endDate=(Date)longHolidayEdituiBean.txtendDate.getValue();
        	Time startTime=(Time)longHolidayEdituiBean.txtStartTime.getValue();
        	Time endTime=(Time)longHolidayEdituiBean.txtEndTime.getValue();
        	HolidayTypeInfo typeInfo=(HolidayTypeInfo)longHolidayEdituiBean.prmtHolidayType.getValue();
        	Calendar c = Calendar.getInstance();
        	c.setTime(startDate);
        	Calendar c2 = Calendar.getInstance();
        	c2.setTime(endDate);
        	//同步前端修改到后台
        	updataModel();
       	    List<CoreBaseInfo> list=getListModel();
       	    List resultList=new LinkedList<CoreBaseInfo>();
       	    HashMap map = new HashMap();
        	for(int i=0;i<list.size();i++)
        	{
        		HolidayBillEntryInfo entryInfo=(HolidayBillEntryInfo)list.get(i);
        		map.put(entryInfo.getBizDate(), entryInfo);
        	}
        	Iterator its=map.entrySet().iterator();
        	while(its.hasNext())
        	{
        		Entry next = (Entry) its.next();
        		HolidayBillEntryInfo entryInfo =(HolidayBillEntryInfo)next.getValue();
        		resultList.add(entryInfo);
        	}
        	this.listModel=resultList;
            Collections.sort(listModel, new Comparator() {
                public int compare(Object o1, Object o2) {
                  return ((HolidayBillEntryInfo)o1).getBizDate().compareTo(((HolidayBillEntryInfo)o2).getBizDate());
                }
              });
        	while(c.getTime().compareTo(c2.getTime()) <=0)
        	{
                CoreBaseInfo obj = createNewEntry();
                HolidayBillEntryInfo info=(HolidayBillEntryInfo)obj;
                info.setHolidayType(typeInfo);
                info.setBizDate(c.getTime());
                info.setStartTime(startTime);
                info.setEndTime(endTime);
                info.setTotalhour(new BigDecimal("7.5"));
                getListModel().add(info);     
        		c.add(Calendar.DATE, 1);
        	}
            editGrid.reload();
            //getEdiGrid().repaint();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            String title = Resources.getText("updateEntryModelError");
            String message = e.getMessage();
            if(StringUtils.isEmpty(message))
                 message = title;
            addMessage(title, message);
        }
    }
    
	/**
	 * 重点
	 * F7控件内容预先加载到界面
	 */
	public List<CoreBaseInfo> storeValue(String store)
	  {
		 if ("holidayType".equals(store)) {
		      if (this.holidayTypeList == null) {
		        this.holidayTypeList = getHolidayTypeCollectionStore();
		      }
		      return this.holidayTypeList;
		    }
	    return super.storeValue(store);
	  }
	
	private List getHolidayTypeCollectionStore() {
		// TODO Auto-generated method stub
		
		List typeList = new LinkedList();
	    Context  ctx = WafContext.getInstance().getContext();
	    
	    SelectorItemCollection sic = new SelectorItemCollection();
		sic.add("id");
		sic.add("*");
		EntityViewInfo view = new EntityViewInfo();
		view.setSelector(sic);
		try {
			HolidayTypeCollection  col = HolidayTypeFactory.getLocalInstance(ctx).getHolidayTypeCollection(view);
			Iterator  itr=col.iterator();
			while(itr.hasNext()){
				typeList.add(itr.next());
			}
		} catch (BOSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return typeList;
	}
	
}

